package com.mantbox.channelcall.controller;


import com.alibaba.fastjson.JSON;
import com.mantbox.channelcall.service.CallService;
import com.mantbox.channelcall.utils.ResResult;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;


@RestController
@RequestMapping("/call")
public class CallControllder {

    @Autowired
    CallService callService;

    @RequestMapping("/test")
    public ResResult test() {
        return ResResult.isOk("成功","test");
    }

    /*回调*/
    @RequestMapping("/call")
    public ResResult reportCall(@RequestParam(required = false) String uid,
                                @RequestParam(required = false) String idfa,
                                @RequestParam(required = false) String urlId,
                                @RequestParam(required = false) String rqId,
                                @RequestParam(required = false)String ts,
                                @RequestParam(required = false) String id,
                                @RequestBody(required = false) Map map) {
        String remark = "";
        if (map != null) {
            remark = JSON.toJSONString(map);
        }
        String err = "成功";
        boolean isTrue = true;
        try {
            callService.saveCall(idfa,uid,rqId,Integer.parseInt(urlId),ts,remark, Integer.parseInt(id));
        }catch (Exception e) {
            e.printStackTrace();
            err = "错误";
            isTrue = false;
        }finally {
            if (isTrue) {
                return  ResResult.isOk("成功",err);
            }else  {
                return  ResResult.isError(err);
            }

        }
    }

    /*
    * 回调列表
    * page: 开始页面
    * pageSize： 每页长度
    * start： 开始事件 yyyy-MM-dd
    * end： 结束时间 yyyy-MM-dd
    * */
    @RequestMapping("/pageList")
    public ResResult pageList(Integer page, Integer pageSize, String start, String end) {
        return ResResult.isOk("成功",callService.pageList(page,pageSize,start,end));
    }
}
